package com.talent.loginservice.impl;

import com.talent.entities.dto.Admin;
import com.talent.entities.dto.User;
import com.talent.entities.vo.LoginUser;
import com.talent.loginmapper.LoginMapper;
import com.talent.loginservice.LoginService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author 周意朗
 * Date: 2022/4/10
 * @since JDK 11
 */
@Service
public class LoginServiceImpl implements LoginService {
    @Resource
    public LoginMapper mapper;

    @Override
    public User loginMethod(LoginUser user) {
        User user1 = mapper.loginMethod(user);
        if (user1 != null) {
            if (!Objects.equals(user.getPwd(), user1.getUserPwd())) {
                user1.setUserTel("0000");
            }
            return user1;
        }
        return null;
    }

    @Override
    public Admin loginMethodAdmin(Admin admin) {
        Admin admin1 = mapper.loginMethodAdmin(admin);
        if (admin1 != null) {
            if (!Objects.equals(admin.getAdminPwd(), admin1.getAdminPwd())) {
                admin1.setAdminPhone("0000");
            }
            return admin1;
        }
        return null;
    }

    @Override
    public Boolean updateUser(User user) {
        return mapper.updateUser(user.getUserTel(), user.getUserEmail(), user.getUserAreaId());
    }

    @Override
    public User afterUpdateUser(String tel) {
        return mapper.afterUpdateUser(tel);
    }

    @Override
    public Boolean updateUserPassword(String tel, String pwd, String id) {
        String findId = updateUserPasswordHelper(tel);
        if (findId.equals(id)) {
            return mapper.updateUserPassword(tel, pwd, id);
        } else {
            return null;
        }
    }

    @Override
    public String updateUserPasswordHelper(String tel) {
        return mapper.updateUserPasswordHelper(tel);
    }

    @Override
    public Boolean registUser(User user) {
        Integer f1 = beforeRegistID(user.getUserId());
        Integer f2 = beforeRegistTel(user.getUserTel());
        if ((f1 + f2) > 0) {
            return false;
        }
        mapper.registUserDropUserOrderMapping(user.getUserId());
        mapper.registUserCreateUserOrderMapping(user.getUserId());
        return mapper.registUser(user);
    }

    @Override
    public Integer beforeRegistID(String id) {
        return mapper.beforeRegistID(id);
    }

    @Override
    public Integer beforeRegistTel(String tel) {
        return mapper.beforeRegistTel(tel);
    }
}
